# -*- coding: utf-8 -*-
#   __author__ = "Casey"     Email = 395280963qq.com
#   Data :2019/11/14  Python:3.7

import requests
import time,re
from requests.exceptions import RequestException
from pyquery import PyQuery as pq
s = requests.session()

"""需要手动更换cookie    其中有些参数需要自己的账户信息，作者用****代替，需要自己去抓包替换
"""
def doLogin():
    """
    模拟登录  维持cookie
    :return:
    """
    login_url = "https://passport.jd.com/uc/loginService?uuid=dc6e3fef-7888-4904-9ab0-7f4d28c02b4c&ltype=logout&r=0.1448401382566069&version=2015"
    data = {
        'uuid': 'dc6e3fef-7888-4904-9ab0-7f4d28c02b4c',
        'eid': 'GPJ6XKYNGTVODQIBFOHODLBRJ6KEOBVBQ4N6O4W3KOZ6P2BO46KJQXFAGPRSYBFTCJRTYN3QP3ONEONGQTAUHAV7BY',
        'fp': '5a141c034b9926b8b48be14efce0765d',
        '_t':'_t',
        'loginType': 'c',
        'loginname': '*******',
        'nloginpwd': '*******',
        'authcode':'*******',
        'pubKey': '********',
        'sa_token': 'B68C442BE645754F33277E701208059080DD726A94A73F76DEC3053A838549C06EB7D3797CE1C5BBE7C2B2EF9CA7D467C4B841002F8A2DEA0DD3ACC5BA959244A539E8B9D2CB176B62FE436B89B8310978A5D9B99A4545B16012582BCDF61F8FC7A8AE9D3A8CC2F220C2541A6395F39025E34AF37F7AC4996E097D59805880B3433423EAC0F6B09DF302DF2D79502CDAA1528E521CF020D4343C933BA246CA2B7F0E28EFEF5447C5528E5003F5198E1F903B62A29E624A2A83113899EF589E6B562BC88FD749267751228ECA3944E2138193D908E42CE70ADFB7FEFC3FEA2141FD4873B2D875CE2EBC969A291FE32659A4F4ACD7FA181560B912602C913D206E7873E0F26E8D8935F705ADE742EC1C91FD2F55088935B2C228F3BF5CE0E5200736BCB47AD6DE4F06996FBE2317CF4527BB0AB5990834277D9F45CD889670212E8E3DCFDE144D92C37DE7124F3FD5761D113B50BCAC2BC0400FC3A0C64B88CEE311BD8718EF1F618D4A04749F3EE08BF96DBE67A4BC4BB06EF74234F5CBC693A5E737125B54CFC13BD8E7FCBFBFF21FAFFF9DC8F563CD185CD059227156D9C7C4B6FA40F4544CD4102311CD134C7075D8AB2EE6661A339533F8364EF2958B5FD5EE086EF6F0ABD257F0F2EFC32669AB99DE5AD00DA39261A0D5D6A57FF4B04056BBD7B0F665ACD7670F275B4B050D37F0',
        'seqSid': '6370108619386947400',
        'useSlideAuthCode': '1',
    }
    res = s.post(login_url, data=data)
    if res.status_code==200:
        return res.text
    else:
        return None

def shopHome():
    """
    解析主页的标题
    :return:
    """
    url = "https://cart.jd.com/cart.action"
    res = s.get(url)
    html = res.content.decode("utf-8")
    print(re.findall("<title>(.*?)</title>", html))

def getPage(url):
    """
    :param 解析网页:
    :return 网页文本内容:
    """
    try:
        headers = {
            'cookie':"__jdu=15653220381511729615320; shshshfpa=1037feb2-751c-b29a-f72f-b0a52af926a2-1565322038; shshshfpb=i%2FZKRBV7BPVnIuYrOPZEu3Q%3D%3D; ipLoc-djd=1-72-4137-0; areaId=1; __jda=122270672.15653220381511729615320.1565322038.1565322038.1573720834.2; __jdv=122270672|direct|-|none|-|1573720833943; __jdc=122270672; _gcl_au=1.1.1966938769.1573720835; wlfstk_smdl=u2sl4jmnfxskay9zzcdahiy1bmi59ps4; TrackID=14nl5vH_MNr662E9GemYEAamXs4bQvx5KJQa7mmbEPOTqcs3r0t2uIEXEpP5R2WByV0F1V35ybIyHheRDBSxKz0xfmJzmm6WpKyEZwqkvQyI; thor=9C0AF0E9DF975056B4FB16D4E4E07BBF99129694CA89963048142AD5F68A41B061A0A80CE3C504CAFCCDAEA2FE28C02182AF7312F4266575C112FAEC9693331F9338CE81F321C5041F0BA9C0DDF5DA3549A933E023397D90612C7D2F7AA48BDFCB82B3E760DFAA475F338B4035D0DEFC94C5E76AB361E5A0D422761DD0BCDEB0654A18D38C2CCA90B073A21C0AB4232A; pinId=S-kI436DkXpltaGFGnacAQ; pin=longshao361; unick=bestlman; ceshi3.com=000; _tp=8UrnIifWAUSKN2LGYpNbtw%3D%3D; _pst=longshao361; user-key=39f79c93-b89c-4026-8ec1-d3238f4f914d; cd=0; shshshfp=860de4e09489fc8e1130e386107e3560; cart-main=xx; cn=1; __jdb=122270672.6.15653220381511729615320|2.1573720834; shshshsID=99580a52c5a9d2c2494a3617e86cd474_2_1573721146955; 3AB9D23F7A4B3C9B=HBETUINSDTVZIGQOIZIWVQFH754FBPWJP4Y6JJZVXDXGR5AYLDWRLSL4JGNCD3F5NDW4MYCWHK72524P34DGZQSOLI",
            'User - Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6735.400 QQBrowser/10.2.2328.400',
        }
        res = s.get(url,headers=headers)
        if res.status_code == 200:
            print('登录成功！')
            return res.text
        else:
            print('登录失败！')
            return None
    except RequestException:
        return None

def parsePage(content):
    """
    :param 解析的文本内容:
    :return:
    """
    doc = pq(content)
    items = doc("div.item-form")
    for item in items.items():
        yield {
            'title': item.find('div.p-name a').text(),
            'price': item.find('p strong').text(),
        }
def main():
    """
    主函数
    :return 数据内容:
    """
    url = 'https://cart.jd.com/cart.action'
    html = getPage(url)
    for item in parsePage(html):
        print(item)

if __name__ == '__main__':
    print('登录中.....')
    doLogin()
    time.sleep(2)   #延迟2秒进行下一个操作
    shopHome()
    main()
